home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-03-13 | 3.9 KB | 225 lines | [TEXT/CWIE] |
- /*
-
- Macintosh Developer Technical Support MP Sample Code
-
- MPSortTask Modified by Matthew Xavier Mora
- to show MP sorting and Blitting preemptively
-
- mxmora@apple.com
- last modified 1/20/97
-
-
- */
-
- #include "SortPicts.h"
- #include <stdio.h>
- #include <LowMem.h>
- //#include <DriverServices.h>
-
- extern Boolean gUseCoplandTasks;
- extern short gTaskNumber;
-
-
- void SortPicts::MakeThreaded( void)
- {
- ThreadID tempThreadInfo;
- OSErr errWhatErr;
-
- unitCount = newUnitCount = 1;
-
- startTime = TickCount();
-
- if ( !coplandTask )
- {
- errWhatErr = NewThread( kCooperativeThread,
- (ThreadEntryProcPtr) SortPictsThreadEntry,
- (void *)this,
- 20000,
- kCreateIfNeeded,
- (void**)nil,
- &tempThreadInfo);
-
- threadInfo = tempThreadInfo;
-
- YieldToThread( threadInfo);
- } else {
- // KernelProcessID ourKPID;
- OSStatus errWhatTaskErr;
-
- // • Get our Kernel Process ID
- // ourKPID = CurrentKernelProcessID();
-
- // • Make a task ID...
- // sprintf( (char*)&myTaskName, "T%03.3d", gTaskNumber++ );
-
- // • Make sure the info on our pixels is current.
- sortPixmap = GetGWorldPixMap( sortGWorld);
- LockPixels( sortPixmap);
- sortPixels = (SortPixelPtr) GetPixBaseAddr( sortPixmap);
- HLock( (Handle) sortHandle);
- sortData = *sortHandle;
-
- //UseSortData();
- //UnuseSortData();
-
- errWhatTaskErr = MPCreateTask( (TaskProc)SortPictsThreadEntry,
- (void *)this,
- 0,
- NULL,
- 0,
- 0,
- 0,
- &myTaskID);
-
-
- (void)MPCreateCriticalRegion(&sortBlitterBusy);
-
-
- }
- }
-
-
- pascal voidPtr SortPictsThreadEntry( void *there)
- {
- SortPicts *t = (SortPicts *)there;
-
- t->Entry();
-
- return nil;
- }
-
-
- void SortPicts::Entry( void)
- {
- while( 1)
- {
- Scramble();
- Update();
- Sort();
-
- if ( coplandTask ) {
- //DelayFor( kDurationSecond );
- Yield();
- //MPYield();
- }
- }
- }
-
-
- /*
- * SetSortItem
- *
- */
- void SortPicts::SetSortItem( long index, long data)
- {
- //static long horiz, vert;
- long offset;
- //static long lastHorizLeft = 0, lastHorizRight = 0;
- //static long pixelVert;
-
- // #ifdef USE_OPT_SETSORTITEM
- // if( index < low)
- // low = index;
- // if( index > high)
- // high = index;
- // #else
-
- if ( sortData != *sortHandle )
- DebugStr( "\pSetSortItem: SortData is corupt." );
- #if 0
- horiz = index % pictWidth;
- vert = index / pictWidth;
-
- if( vert >= copyBitsRect.bottom)
- copyBitsRect.bottom = vert+1;
-
- if( vert < copyBitsRect.top)
- copyBitsRect.top = vert;
-
- offset = horiz + vert * pictRowBytes;
- #else
- if( index > lastHorizLeft && index < lastHorizRight)
- {
- }
- else
- {
- vert = index / pictWidth;
- lastHorizLeft = vert * pictWidth;
- lastHorizRight = lastHorizLeft + pictWidth;
-
- pixelVert = vert * pictRowBytes;
-
- if( vert >= copyBitsRect.bottom)
- copyBitsRect.bottom = (short) vert+1;
-
- if( vert < copyBitsRect.top)
- copyBitsRect.top = (short) vert;
- }
- horiz = index - lastHorizLeft;
-
- offset = horiz + pixelVert;
- #endif
- sortPixels[offset] = (SortPixel) data;
- //pixelBufferData[offset
-
- sortData[index] = data;
-
- if(!unitCount) {
- Yield();
- }
- unitCount--;
-
- }
-
- void SortPicts::ExchangeSortItem( long one, long theOther)
- {
- long temp;
-
- temp = sortData[one];
- SetSortItem( one, sortData[theOther]);
- SetSortItem( theOther, temp);
- if (coplandTask) { // Call this to simulate Idle
- Idle(nil);
- }
- }
-
-
- void SortPicts::Yield( void)
- {
-
- if ( ! coplandTask )
- {
- CalculateNewUnitCount();
- Update();
- UnuseSortData();
- YieldToAnyThread();
- UseSortData();
- startTime = TickCount();
- } else {
- MPYield();
- }
-
- }
-
- void SortPicts::CalculateNewUnitCount( void)
- {
- long deltaTime ;
-
-
- if (coplandTask) {
- deltaTime = LMGetTicks() - startTime;
-
- } else {
- deltaTime = LMGetTicks() - startTime;
- }
-
- if( deltaTime)
- newUnitCount = (int) (((double) newUnitCount) *
- ((double) kYieldTime / (double) deltaTime));
- else
- newUnitCount = newUnitCount * 6 + 1;
-
- unitCount = newUnitCount;
- }
-
-